/**   
 * 文件名：DateUtils.java
 *   
 * 版本信息：   
 * 日期：Mar 4, 2010   
 * Copyright 2010 JHSYS     
 * 版权所有   
 *   
 */
package com.anjubao.parking.municipal.roadside.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;


public class DateUtils {
	private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

	private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

	private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");

	private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private final static SimpleDateFormat sdfWeekTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEEE");

	private final static SimpleDateFormat sdfNoSignTime = new SimpleDateFormat("yyyyMMddHHmmss");

	private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	/**
	 * log4j日志 
	 */
	static private Log log = LogFactory.getLog("DateUtils.class");
	/**   
	  
	 * 把这种形式yyyy-MM-dd HH:mm:ss的字符串转换成时间类型。   
	  
	 * @param   dateStr   这种形式yyyy-MM-dd HH:mm:ss的字符串
	  
	 * @return 时间类型   Date  
	  
	 * @Exception 异常对象   
	  
	 * @since  CodingExample　Ver(编码范例查看) 1.1   
	  
	*/
	static public  Date string2Date(String dateStr){
		if(dateStr == null){
			log.info("字符串转换成时间类型不能为空。");
		}
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date newCreateTime = null;
		if(!"".equals(dateStr) ){
			try {
				 newCreateTime = sdf.parse(dateStr);
			} catch (ParseException e) {
				log.warn("时间转换出错!!create_time:"+dateStr);
				e.printStackTrace();
			}
		}
		return newCreateTime;
	}

	static public LocalDateTime stringToLocalDateTime(String formatStr){
		if(formatStr == null){
			log.info("字符串转换成时间类型不能为空。");
			return null;
		}
		LocalDateTime localDateTime = LocalDateTime.parse(formatStr,dateTimeFormatter);
		return localDateTime;
	}

	static public LocalDateTime dataToLocalDateTime(Date date){
		if(date != null){
			String dateStr = DateUtils.getDateStr(date);
			LocalDateTime localDateTime = DateUtils.stringToLocalDateTime(dateStr);
			return  localDateTime;
		}else {
			log.info("Date类型转换成时间类型不能为空。");
			return null;
		}

	}

	static public String getDateStr(Date date) {
		if (date != null){
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
	        return format.format(date);    
		}
		else{
			return null;
		}
    }    
	
	static public String getDateStr(Date date,String formatStr){
		if (date != null){
			SimpleDateFormat format = new SimpleDateFormat(formatStr);   
			return format.format(date);
		}
		else {
			return null;
		}
	}

	public static Date getDate(String relatedTime, String formatD) {
		  SimpleDateFormat format = new SimpleDateFormat(formatD);    
		  try {
			  if (relatedTime != null){
				  return format.parse(relatedTime);
			  }
		} catch (ParseException e) {
			e.printStackTrace();
		}
		  return null;
	}	
	
	/**
	 * 将日期字符串转化为特定格式的字符串
	 * @author liuqinghui
	 * @param dateStr  日期字符串
	 * @param formatStr  
	 * @return
	 */
	public static String getStrFormStr(String dateStr,String formatStr){
		Date newDate = string2Date(dateStr);//转换为日期格式
		
		String newStr = getDateStr(newDate, formatStr);//转换为字符串
		
		return newStr;
	}

	/**
	 * 得到n天之后的日期(yyyy-mm-dd)
	 *
	 * @param days
	 * @return
	 */
	public static Date getAfterDayDateString(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();
		return date;
	}
	/**
	 * 得到n天之后的日期(yyyy-mm-dd)
	 *
	 * @param days
	 * @return
	 */
	public static String getAfterDayDateStr(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();
		String newStrDate = getDateStr(date, "yyyy-MM-dd");//转换为字符串
		return newStrDate;
	}
	/**
	 * 得到n天之后的日期(yyyy-mm-dd)
	 *
	 * @param
	 * @return
	 */
	static public String getTimeStr(Date date) {
		if (date != null){
			SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
			return format.format(date);
		}
		else{
			return null;
		}
	}

	/**
	 * 得到n天之后的日期(yyyy-mm-dd)
	 *
	 * @param
	 * @return
	 */
	static public Date getSysDate() {

			return new Date();
	}
	/**
	 * 得到n天之后的日期(yyyy-mm-dd)
	 *
	 * @param
	 * @return
	 */
	static public String getCurrentTimeStr() {
			return sdfTime.format(new Date());
	}

	/**
	 * 获取距离当前时间的前或者后几天
	 * @param day
	 * @return
	 */
	public static Date getSpecificDay(int day){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, day);
		return calendar.getTime();
	}

}
